<?php 

	define( "HOST" , 'localhost' );
	define( "DBNAME" , "scutter_f" );
	define( "DBPASS" , "rwhe08" );
	define( "DATABASE" , "scutter" );
	
	function sql_select($query){
		@ $con = mysql_connect(HOST,DBNAME,DBPASS) or die('无法连接数据库！' );
		mysql_select_db(DATABASE);
		$result = mysql_query($query);
		$i = 0;
		while($info[$i++] = mysql_fetch_array($result));
		unset($info[$i-1]);
		return $info;
	}
	
	/** 获取当前时间戳，精确到毫秒 */

	function microtime_float()
	{
	   list($usec, $sec) = explode(" ", microtime());
	   return ((float)$usec + (float)$sec);
	}

	/** 格式化时间戳，精确到毫秒，x代表毫秒 */

	function microtime_format($tag, $time)
	{
	   list($usec, $sec) = explode(".", $time);
	   $date = date($tag,$usec);
	   return str_replace('x', $sec, $date);
	}
		
	function genRandomString($len){
		$chars = array(
			"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
			"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
			"w", "x", "y", "z", "0", "1", "2", "3", "4", "5"
		);
		$charsLen = count($chars) - 1;
		shuffle($chars);    // 将数组打乱
		$output = "";
		for ($i=0; $i<$len; $i++)
		{
			$output .= $chars[mt_rand(0, $charsLen)];
		}
		return $output;
	}
	
	function login($jwuser,$jwpass){
		//echo $jwuser.$jwpass;exit();
		if(strlen($jwuser)!=12) return array();
		$key = genRandomString(24);
		$URL="http://222.201.132.68/(".$key.")/default2.aspx";
		$AdminUrl="http://222.201.132.68/(".$key.")/xs_main.aspx?xh=".$jwuser;
		$post_data['__VIEWSTATE'] = "dDwtMTIwMTU3OTE3Nzs7PpB8l5%2FEl8v0pRD42cfBxZlfb5jx";
		$post_data['TextBox1'] = $jwuser;
		$post_data['TextBox2'] = $jwpass;
		$post_data['RadioButtonList1'] = "%D1%A7%C9%FA";
		$post_data['Button1'] = "";
		$post_data['lbLanguage'] = "";
		$referrer="http://222.201.132.68/(".$key.")/default2.aspx";
		// parsing the given URL
		$URL_Info=parse_url($URL);
		// Building referrer
		if($referrer=="") // if not given use this script as referrer
			$referrer=$_SERVER["SCRIPT_URI"];
		 
		// making string from $data
		foreach($post_data as $k=>$value)
		//	$values[]="$k=".urlencode($value);
			$values[]="$k=".$value;
		 
		$data_string=implode("&",$values);
		// Find out which port is needed - if not given use standard (=80)
		if(!isset($URL_Info["port"]))
			$URL_Info["port"]=80;
		// building POST-request:
		$request.="POST ".$URL_Info["path"]." HTTP/1.1\n";
		$request.="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\n";
		$request.="Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3\n";
		$request.="Accept-Encoding: gzip,deflate,sdch\n";
		$request.="Accept-Language: zh-CN,zh;q=0.8\n";
		$request.="Cache-Control: max-age=0\n";
		//$request.="Connection: keep-alive\n";
		$request.="Content-Type: application/x-www-form-urlencoded\n";
		$request.="Host: ".$URL_Info["host"]."\n";
		$request.="Origin: http://222.201.132.68\n";
		$request.="Referer: $referrer\n";
		$request.="Content-length: ".strlen($data_string)."\n";
		$request.="User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko)\n";
		//$request.="Chrome/15.0.874.121 Safari/535.2\n";
		$request.="Connection: close\n";
		$request.="\n";
		$request.=$data_string."\n";
		$fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
		fputs($fp, $request);
		while(!feof($fp)) {
			$result .= fgets($fp, 128);
		}
		fclose($fp);
		$result = strip_tags($result);
		$result = substr($result,-18,-5);
		$baseinfo = array();
		if($result=="moved to here"){
			$baseinfo["key"] = $key;
			$baseinfo["jwuser"] = $jwuser;
		}
		return $baseinfo;
	}
	
	function getmygarde($baseinfo){
		$jwuser = $baseinfo["jwuser"];
		$key = $baseinfo["key"];
		$baseurl = "http://222.201.132.68/(".$key.")/";
		$adminurl =  $baseurl."xs_main.aspx?xh=".$jwuser;
		$SeekUrl = $baseurl."xscjcx.aspx?xh=".$jwuser;
		//$content = file_get_contents($SeekUrl);
		//echo $content;
		$request.="GET /(".$key.")/xscjcx.aspx?xh=".$jwuser." HTTP/1.1\n";
		$request.="Host: 222.201.132.68\n";
		$request.="User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2\n";
		$request.="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\n";
		//$request.="Connection: keep-alive\n";
		$request.="Referer: http://222.201.132.68/(".$key.")/xs_main.aspx?xh=".$jwuser."\n";
		$request.="Accept-Encoding: gzip,deflate,sdch\n";
		$request.="Accept-Language: zh-CN,zh;q=0.8\n";
		$request.="Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3\n";
		$request.="Cookie: BIGipServerjwxt_bs_80=1233439198.20480.0000\n";
		//$request.="Chrome/15.0.874.121 Safari/535.2\n";
		$request.="Connection: close\n";
		$request.="\n";
		$fp = fsockopen("222.201.132.68",80);
		fputs($fp, $request);
		while(!feof($fp)) {
			$result .= fgets($fp, 128);
		}
		fclose($fp);
		$result = explode('__VIEWSTATE" value="',$result);
		$result = explode('" />',$result[1]);
		$jw__VIEWSTATE = $result[0];
		$jw__VIEWSTATE = urlencode($jw__VIEWSTATE);
		$post_data = "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=".$jw__VIEWSTATE."&hidLanguage=&ddlXN=&ddlXQ=&ddl_kcxz=&btn_zcj=%C0%FA%C4%EA%B3%C9%BC%A8";
		$request = "";
		$request.="POST /(".$key.")/xscjcx.aspx?xh=".$jwuser." HTTP/1.1\n";
		$request.="Host: 222.201.132.68\n";
		$request.="Content-length: ".strlen($post_data)."\n";
		$request.="Cache-Control: max-age=0\n";
		$request.="Origin: http://222.201.132.68\n";
		$request.="User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2\n";
		$request.="Content-Type: application/x-www-form-urlencoded\n";
		$request.="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\n";
		//$request.="Connection: keep-alive\n";
		$request.="Referer: http://222.201.132.68/(".$key.")/xscjcx.aspx?xh=".$jwuser."\n";
		$request.="Accept-Encoding: gzip,deflate,sdch\n";
		$request.="Accept-Language: zh-CN,zh;q=0.8\n";
		$request.="Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3\n";
		$request.="Cookie: BIGipServerjwxt_bs_80=1233439198.20480.0000\n";
		//$request.="Chrome/15.0.874.121 Safari/535.2\n";
		$request.="Connection: close\n";
		$request.="\n";
		$request.=$post_data."\n";
		$fp = fsockopen("222.201.132.68",80);
		fputs($fp, $request);
		while(!feof($fp)) {
			$result .= fgets($fp, 128);
		}
		fclose($fp);
		//$result = strip_tags($result);
		$result = explode('table class="datelist"',$result);
		$result = explode('</table>',$result[1]);
		$result = mb_convert_encoding($result[0],"utf-8","gbk");
		$result = explode('<tr',$result);
		$i = 0;$list = array();
		foreach($result as $k => $line){
			$line = explode('>',$line,2);
			$line[1]=preg_replace("/\s+/",'',$line[1]);
			$list[$i] = explode('</td>',$line[1]);
			foreach($list[$i] as $m => $one){
				$list[$i][$m] = strip_tags($one);
			}
			if(!$list[$i][1]) unset($list[$i]);
			else $i++;
		}
		return $list;
	}
	
	function showmygrade($gradelist){
		unset($gradelist[0]);
		arsort($gradelist);
		foreach($gradelist as $k => $line){
			$text .= $line[2].":".$line[3].":".$line[8]." => "; 
		}
		$text = substr($text,0,-3);
		echo $text;
	}
	if($_GET['jwuser']){
		$jwuser = $_GET['jwuser'];
		$query = "SELECT `jw_pass` FROM `sina_jw2005` WHERE `jw_user`='$jwuser'";
		$result = sql_select($query);
		$jwpass = $result[0][0];
		//$t1 = microtime_float();
		$baseinfo = login($jwuser,$jwpass);
		//echo "抓首页：".(microtime_float()-$t1)."<br><br>";
		$gradelist = getmygarde($baseinfo);
		showmygrade($gradelist);
		//$t2 = microtime_float();
		//echo "<br>总时间：".($t2-$t1);
	}
?>